<?php
namespace app\merchant\controller;


use think\Controller;

class Login extends Controller
{

    //管理登陆
    public function login(){
        return $this->fetch();
    }

    //用户登陆认证
    public function loginCheck(){
        $name = input('param.name');
        $pwd = input('param.pwd');
//        $save = input('param.save');

        $user = model('Merchant')->where('username',$name)->find();
//        $userinfoObj = $user->where('aname', $name)->find();
        if (empty($user)) {
            return json( ['code' => 400, 'msg' => '账号不存在']);
        } else {
            if ($user['status'] != 1) {
                return json( ['code' => 400, 'msg' => '该账号已停用，如有疑问请联系客服']);
            }
            $md5Pwd = $pwd;
            if ($md5Pwd === $user['password']) {

                session('aid', $user['id']);
                return json(['code'=>200,'msg'=>'登录成功']);
            } else {
                return json( ['code' => 400, 'msg' => '密码错误']);
            }
        }
    }

    //退出
    public function loginOut(){
        session('aid',null);
        cookie('aid',null);
        return json(['code'=>200,'msg'=>'退出成功']);
    }

    //退出
    public function subLoginOut(){
        session('subid',null);
        cookie('subid',null);
        return json(['code'=>200,'msg'=>'退出成功']);
    }

    /* 空操作，用于输出404页面 */
    public function _empty(){
        $this->redirect('Index/index');
    }


    //管理登陆
    public function subLogin(){
        return $this->fetch();
    }

    //用户登陆认证
    public function subLoginCheck(){
        $name = input('param.name');
        $pwd = input('param.pwd');
//        $save = input('param.save');

        $user = model('MerchantSub')->where('username',$name)->find();
//        $userinfoObj = $user->where('aname', $name)->find();
        if (empty($user)) {
            return json( ['code' => 400, 'msg' => '账号不存在']);
        } else {
            if ($user['status'] != 1) {
                return json( ['code' => 400, 'msg' => '该账号已停用，如有疑问请联系客服']);
            }
            $md5Pwd = $pwd;
            if ($md5Pwd === $user['password']) {

                session('subid', $user['id']);
                return json(['code'=>200,'msg'=>'登录成功']);
            } else {
                return json( ['code' => 400, 'msg' => '密码错误']);
            }
        }
    }

}
